#include <FileHandler.h>

#include <IFormatter.h>

namespace logging
{
    bool FileHandler::open( const std::string & fileName, bool append )
    {
	close();

	std::ios_base::openmode flags = std::ios_base::out | (append ? std::ios_base::app : std::ios_base::trunc );

	m_stream.open( fileName.c_str(), flags );
	return( m_stream.is_open() );
    }

    void FileHandler::close()
    {
	m_stream.close();
    }
    
    void FileHandler::flush()
    {
	m_stream.flush();
    }
	    
    LogLevel FileHandler::logLevel() const
    {
	return( m_logLevel );
    }

    void FileHandler::setLogLevel( LogLevel logLevel )
    {
	this->m_logLevel = logLevel;
    }

    void FileHandler::processRecord( const LogRecord & record )
    {
	if( m_formatter == 0 )
	    return;

	if( !m_stream.is_open() )
	    return;

	if( m_logLevel < record.m_logLevel )
	    return;

	m_stream << m_formatter->format( record ) << std::endl;
    }

    bool FileHandler::ready()
    {
	return( m_stream.is_open() );
    }
}

